/*******************************************************************************
 * Copyright 2011, 2012 Chris Banes.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *******************************************************************************/
package travlefate.story.views;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.handmark.pulltorefresh.library.PullToRefreshBase;

import travlefate.story.utils.Loger;

public class PullToRefreshRecycleView extends PullToRefreshBase<RecyclerView>{

	private static final String TAG = "PullToRefreshRecycleView";
	private boolean showLog = false;
	private LastVisibleListener lastVisibleListener;
	private RecyclerView mRecycle;

	public PullToRefreshRecycleView(Context context) {
		super(context);
	}

	public PullToRefreshRecycleView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PullToRefreshRecycleView(Context context, Mode mode) {
		super(context, mode);
	}

	public PullToRefreshRecycleView(Context context, Mode mode, AnimationStyle style) {
		super(context, mode, style);
	}

	@Override
	public Orientation getPullToRefreshScrollDirection() {
		return Orientation.VERTICAL;
	}

	@Override
	protected RecyclerView createRefreshableView(Context context, AttributeSet attrs) {
		mRecycle = new RecyclerView(context, attrs);
		mRecycle.setId(android.R.id.list);
		mRecycle.addOnScrollListener(new RecyclerView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
				super.onScrollStateChanged(recyclerView, newState);
				LinearLayoutManager lm = (LinearLayoutManager) recyclerView.getLayoutManager();
				RecyclerView.Adapter adapter = recyclerView.getAdapter();
				if (newState == RecyclerView.SCROLL_STATE_IDLE && null != lm && null != adapter) {
					if (lm.findLastVisibleItemPosition() + 1 == adapter.getItemCount()) {
						if (lastVisibleListener != null) {
							lastVisibleListener.onLastVisibleListener();
						}
					}
				}

			}

			@Override
			public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
				super.onScrolled(recyclerView, dx, dy);
			}
		});
		return mRecycle;
	}

	@Override
	protected boolean isReadyForPullEnd() {
		return isLastItemVisible();
	}

	@Override
	protected boolean isReadyForPullStart() {
		return isFirstItemVisible();
	}

	private boolean isLastItemVisible() {
		final RecyclerView.Adapter adapter = mRefreshableView.getAdapter();

		if (null == adapter) {
			return true;
		} else {
			if(adapter.getItemCount()==0){
				return false;
			}
			LinearLayoutManager lm= (LinearLayoutManager) mRefreshableView.getLayoutManager();
			if(lm.findLastVisibleItemPosition()+1==adapter.getItemCount()){
				if (showLog) {
					Loger.i(TAG,"isLastItemVisible"+lm.findLastVisibleItemPosition());
				}

				return true;
			}
		}
		return false;
	}

	private boolean isFirstItemVisible() {

		final RecyclerView.Adapter adapter = mRefreshableView.getAdapter();

		if (null == adapter ) {
			return true;
		} else {
			if(adapter.getItemCount()==0){
				return true;
			}
			LinearLayoutManager lm = (LinearLayoutManager) mRefreshableView.getLayoutManager();
			if (lm.findFirstVisibleItemPosition() <= 1) {
				View firstVisibleChild = mRefreshableView.getChildAt(0);
				if (firstVisibleChild != null) {
					if (showLog) {
						Loger.i(TAG,"isFirstItemVisible:"+lm.findFirstVisibleItemPosition());
					}
					return firstVisibleChild.getTop() >= mRefreshableView.getTop();
				}
			}
		}
		return false;
	}

	public void setOnlastVisibleListener(LastVisibleListener lastVisibleListener){
		this.lastVisibleListener =lastVisibleListener;
	}



	public interface LastVisibleListener{
		void onLastVisibleListener();
	}

	public void setLayoutManager(RecyclerView.LayoutManager layoutManager){
		mRecycle.setLayoutManager(layoutManager);
	}

	public void setAdapter(RecyclerView.Adapter adapter){
		mRecycle.setAdapter(adapter);
	}
}
